package com.fcscs.cservice.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.common.CServiceCommonEnums.PriceCategoryCodeEnum;
import com.fcscs.cservice.dao.InRoomItemPriceDao;
import com.fcscs.cservice.domain.InRoomItemPrice;

@SuppressWarnings("unchecked")
@Repository("inRoomItemPriceDao")
public class InRoomItemPriceDaoHibernate extends BaseDaoHibernate<InRoomItemPrice> implements InRoomItemPriceDao {

	private static DBLoc DBLoc = new InRoomItemPrice().getDBLoc();

	@Autowired
	private ConnectionManager connMgr;

	private static final String NAME_CNT = "from InRoomItemPrice where itemId = :itemId";
	
	private static final String FIND = "from InRoomItemPrice where itemId = :itemId and  priceCategory = :price";

	@Override
	protected Class<InRoomItemPrice> getCls() {
		return InRoomItemPrice.class;
	}

	@Override
	protected ConnectionManager getConnectionManager() {
		return connMgr;
	}

	@Override
	protected DBLoc getDBLoc() {
		return DBLoc;
	}

	@Override
	public <E extends InRoomItemPrice> E get(Class<E> cls, long id) {
		return (E) getConnectionManager().getCurrentSession(getDBLoc()).load(cls, id);
	}

	@Override
	public <E extends InRoomItemPrice> void save(E loc) {
		getConnectionManager().getCurrentSession(DBLoc).saveOrUpdate(loc);
	}

	@Override
	public List<InRoomItemPrice> getInRoomItemPrices(long inRoomItemId) {
		Query q = connMgr.getCurrentSession(DBLoc).createQuery(NAME_CNT);
		q.setLong("itemId", inRoomItemId);
		return q.list();
	}

	@Override
	public InRoomItemPrice getInRoomItemPrice(long inRoomItemId, PriceCategoryCodeEnum price) {
		Query q = connMgr.getCurrentSession(DBLoc).createQuery(FIND);
		q.setLong("itemId", inRoomItemId);
		q.setString("price", price.toString());
		return (InRoomItemPrice) q.uniqueResult();
	}

	@Override
	public void permDeletePrices(long itemId) {
		SQLQuery q = connMgr.getCurrentSession(DBLoc).createSQLQuery("delete from in_room_item_price where item_id = :id");
		q.setLong("id", itemId);
		q.executeUpdate();		
	}

}
